home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 076-100 / 079 / sharedlib / 2tasks.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  4KB  |  169 lines

  1. /* 2Tasks.C    James M Synge,    May 18, 1987          */
  2.  
  3. /* Include files */
  4. #include "exec/types.h"
  5. #include "exec/nodes.h"
  6. #include "exec/lists.h"
  7. #include "exec/tasks.h"
  8. #include "exec/libraries.h"
  9. #include "exec/ports.h"
  10.  
  11. /* Other declarations    */
  12.  
  13. struct Library  *TaskBase, /* Library Base Pointer. */
  14.         *OpenLibrary();
  15.  
  16. struct MsgPort    *CreatePort(), *FindPort();
  17. struct Message    *WaitPort(), *GetMsg();
  18.  
  19. #define TASK_LIBRARY    "task.library"
  20. #define    TASK_VERSION    1L
  21.  
  22. #define CHILD_PORT    "2Tasks.Child.Port"
  23. #define CHILD_TASK    "2Tasks.Child.Name"
  24.  
  25. main()
  26. {
  27.     struct Message message, *msg;
  28.     struct MsgPort *ParentPort, *ChildPort;
  29.     struct Task *ChildTask, *CreateTask();
  30.     void ChildMain();
  31.     int seconds;    
  32.  
  33.     /* First things first: Open the task library: */
  34.  
  35.     printf("Openning %s\n", TASK_LIBRARY);
  36.     TaskBase = OpenLibrary(TASK_LIBRARY, TASK_VERSION);
  37.     if (TaskBase == 0L) {
  38.         printf("Unable to open %s\n", TASK_LIBRARY);
  39.         exit(10);
  40.     }
  41.  
  42.     /* Create a nameless MsgPort where we can receive
  43.      * the reply to a message.
  44.      */
  45.     if ((ParentPort = CreatePort( 0L, 0L )) == 0L) {
  46.         printf("Unable to create a MsgPort!\n");
  47.         CloseLibrary( TaskBase );
  48.         exit(10);
  49.     }
  50.  
  51.     /* Now create the child task. */
  52.  
  53.     printf("Creating the child task.\n");
  54.     ChildTask = CreateTask(
  55.         CHILD_TASK,    /* Name of the task.    */
  56.         1L,        /* Higher priority.    */
  57.         ChildMain,    /* Its main routine.    */
  58.         4096L);        /* Stack Size.        */
  59.  
  60.     if (ChildTask == 0L) {
  61.         printf("Unable to create child task!\n");
  62.         DeletePort( ParentPort );
  63.         CloseLibrary( TaskBase );
  64.         exit(10);
  65.     }
  66.  
  67.     /* Find the child's message port. */
  68.  
  69.     for(seconds = 0; seconds < 60; seconds++) {
  70.         printf("Find the child's MsgPort\n");
  71.         if (ChildPort = FindPort( CHILD_PORT ))
  72.             break;
  73.         Delay(50);    /* Wait a second! */
  74.     }
  75.  
  76.     if (ChildPort == 0L) {
  77.         printf("Unable to find child MsgPort!\n");
  78.         DeleteTask( ChildTask );
  79.         DeletePort( ParentPort );
  80.         CloseLibrary( TaskBase );
  81.         exit(10);
  82.     }
  83.  
  84.     /* Send the child a message. */
  85.  
  86.     message.mn_Node.ln_Type    = NT_MESSAGE;
  87.     message.mn_ReplyPort    = ParentPort;
  88.     message.mn_Length    = 0;
  89.  
  90.     printf("Sending the message.\n");
  91.     PutMsg( ChildPort, &message );
  92.  
  93.     /* Wait for the child to respond. */
  94.  
  95.     WaitPort( ParentPort );
  96.  
  97.     /* Get the message. */
  98.  
  99.     msg = GetMsg( ParentPort );
  100.     printf("Got the reply.  All done.\n");
  101.     
  102.     /* And now delete the MsgPort we used. */
  103.  
  104.     DeletePort( ParentPort );
  105.     
  106.     /* Finally, close the library. */
  107.     
  108.     CloseLibrary( TaskBase );
  109.  
  110.     exit(0);
  111. }
  112.  
  113. /* Notice that the child doesn't open any libraries,
  114.  * including exec.library and Task.Library whose
  115.  * routines it uses. It does this because it will
  116.  * operate solely during the life of the parent, when we
  117.  * know the libraries will be open.  This is not kosher in
  118.  * general, but Commodore has produced examples doing this,
  119.  * and I know there aren't any problems in this case.
  120.  *
  121.  * It's also particularly difficult to close a library
  122.  * after using DeleteTask() on yourself!
  123.  */
  124. void ChildMain()
  125. {
  126.     struct Message *msg;
  127.     struct MsgPort *ChildPort;
  128.  
  129.     /* To allow this to be a small code/small data model
  130.      * task, we must make sure register A4 contains the
  131.      * correct value.  We do so by calling the Aztec C
  132.      * routine geta4() which computes the value.
  133.      */
  134.     geta4();
  135.  
  136.     /* Create a MsgPort where we can receive a message
  137.      * from the parent task.
  138.      */
  139.     if ((ChildPort = CreatePort( CHILD_PORT, 0L )) == 0)
  140.         DeleteTask( FindTask( 0L ));
  141.         
  142.     /* Now wait for the message. */
  143.  
  144.     WaitPort( ChildPort );
  145.  
  146.     /* Fetch it from the port... */
  147.  
  148.     msg = GetMsg( ChildPort );
  149.  
  150.     /* ... and reply to it.  Do so inside a Forbid() /
  151.      * Permit() pair so there'll be time to delete the
  152.      * message port.  Note that the Permit() call is
  153.      * not included because it will never be called;
  154.      * instead, DeleteTask() is called so that we
  155.      * delete the current task: ourselves!
  156.      */
  157.     Forbid();
  158.  
  159.     ReplyMsg( msg );
  160.  
  161.     DeletePort( ChildPort );
  162.  
  163.     /* Now take the leap of death. */
  164.  
  165.     DeleteTask( FindTask( 0L ));
  166.  
  167.     /* That's all she wrote! */
  168. }
  169.